package net.zhengxinyun.performance.controller.incentive;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.IncentiveTypeEntity;
import net.zhengxinyun.performance.entity.IncentiveTypeItemEntity;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.filter.IncentiveTypeFilter;
import net.zhengxinyun.performance.service.exportService.ExcelDtoService;
import net.zhengxinyun.performance.service.exportService.ExportService;
import net.zhengxinyun.performance.service.incentive.IncentiveService;
import net.zhengxinyun.performance.service.incentive.IncentiveTypeSerice;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/incentiveType")
public class IncentiveTypeCtrl {
    @Autowired
    private IncentiveTypeSerice typeService;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private LoginService loginService;

    @Autowired
    private ExportService exportService;

    @PostMapping("/queryType")
    public Result<HashMap<String, Object>> query(@RequestBody IncentiveTypeParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            HashMap<String, Object> map = new HashMap<>();
            List<IncentiveTypeFilter> list = typeService.queryType(param.typeEntity);

            // 处理二级数据
            for (IncentiveTypeFilter entity : list) {
                List<IncentiveTypeItemEntity> listItem = typeService.queryItems(entity.getId());
                entity.setItemList(listItem);
            }

            map.put("list", list);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/insertType")
    public Result<String> insertType(@RequestBody IncentiveTypeParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            param.typeEntity.setId(null);
            int str = typeService.insertType(param.typeEntity);
            if (str ==0) {
                return ResultUtils.getResults(Result.Status.ERROR, "添加加分类别失败");
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/updateType")
    public Result<String> updateType(@RequestBody IncentiveTypeParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            int t = typeService.updateType(param.typeEntity);
            if (t ==0) {
                return ResultUtils.getResults(Result.Status.ERROR, "更新加分类别失败");
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token错误");
    }

    @PostMapping("/deleteType")
    public Result<String> deleteType(@RequestBody IncentiveTypeParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            int t = typeService.deleteType(param.typeEntity.getId());
            if (t ==0) {
                return ResultUtils.getResults(Result.Status.ERROR, "更新加分类别失败");
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token错误");
    }


    @PostMapping("/insertItems")
    public Result<String> insertItems(@RequestBody IncentiveTypeParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            List<IncentiveTypeItemEntity> itemList = param.filter.getItemList();
            itemList.forEach(entity -> entity.setTypeId(param.filter.getId()));
            typeService.insertItems(itemList);


            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token错误");
    }

    // 删除具体分类
    @PostMapping("/deleteItems")
    public Result<String> deleteItems(@RequestBody IncentiveTypeParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            Long itemId = param.filter.getItemId();
            if(ObjectUtils.isBlank(itemId))
            {
                return ResultUtils.getResults(Result.Status.ERROR, "itemId为空,请联系管理员");
            }

            int i = typeService.deleteItems(itemId);

            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token错误");
    }

    //修改
    @PostMapping("/updateItems")
    public Result<String> updateItems(@RequestBody IncentiveTypeParam param)
    {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {

            List<IncentiveTypeItemEntity> itemList = param.filter.getItemList();
            if(ObjectUtils.isNotBlank(itemList))
            {
                List<IncentiveTypeItemEntity> saveList = itemList.stream().filter(entity -> ObjectUtils.isBlank(entity.getId())&&StringUtils.isNotBlank(entity.getItemName()) ).collect(Collectors.toList());
                List<IncentiveTypeItemEntity> updateList = itemList.stream().filter(entity -> ObjectUtils.isNotBlank(entity.getId())).collect(Collectors.toList());
                for (IncentiveTypeItemEntity update : updateList) {
                    typeService.updateItems(update);
                }
                // 设置Id
                if(saveList.size()>0)
                {
                    saveList.forEach(entity -> entity.setTypeId(param.filter.getId()));
                    typeService.insertItems(saveList);
                }
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token错误");
    }


    //导入分类
    @PostMapping("/importIncentiveType")
    public  Result<HashMap<String,Object>> importIncentiveType(@RequestParam(value = "file") MultipartFile file){
        if(file ==null)
        {

            return ResultUtils.getResults(Result.Status.ERROR, "上传失败，上传文件不能为空", null);
        }
        String fileName = file.getOriginalFilename();

        if(!fileName.matches("^.+\\.(?i)(xls)$")&&!fileName.matches("^.+\\.(?i)(xlsx)$")){
            return ResultUtils.getResults(Result.Status.ERROR, "上传失败，文件格式错误",null);
        }

        HashMap<String, Object> map = exportService.uploadIncentiveType(file);

        if((int)map.get("code")==500)
        {

          return   ResultUtils.getResults(Result.Status.ERROR,map.get("message").toString(),null);
        }


        return ResultUtils.getSuccessResults(null);
    }


}

class IncentiveTypeParam
{
    public TokenEntity tokenEntity;
    public IncentiveTypeFilter filter;
    public IncentiveTypeEntity typeEntity;
}